package com.example.hxds.common.util;

import org.apache.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: hxds
 * @BelongsPackage: com.example.hxds.common.util
 * @Author: caohaoran
 * @CreateTime: 2025-10-17  09:43
 * @Description: TODO
 * @Version: 1.0
 */
public class R extends HashMap<String, Object> {

    public R() {
        put("code", HttpStatus.SC_OK);
        put("msg", "success");
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this; //返回this,支持链式调用
    }

    public static R ok() {
        return new R();
    }

    public static R ok(String msg) {
        //创建实例，触发默认构造方法（设置code=200，msg="success"）
        R r = new R();
        //覆盖msg字段为传入的msg值
        r.put("msg", msg);
        //返回对象（code保持200，msg为新值）
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        //将指定Map中的所有键值对复制到当前Map中。若当前Map已存在相同键，则覆盖旧值；若不存在。则新增键值对
        r.putAll(map);
        return r;
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static R error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
    }
}
